<?php
class Ftable extends My_Model {

    function __construct()
    {
        parent::__construct();
		$this->load->database();
    }

	function flushTable(){
		$br = "<br>";
		$query = $this->db->query('SHOW TABLES;');
		$tmp_path = realpath(APPPATH);
		foreach ($query->result_array() as $row)
		{
			$table = array_pop($row);
			$dbprefix = $this->db->dbprefix;
			$fields = array();
			$pks = array();
			$rQuery = $this->db->query("SHOW COLUMNS FROM $table");
			foreach ($rQuery->result_array() as $field){
				if(is_object($field)) {
					$field	=	get_object_vars($field);
				}
				$fields[$field['Field']] = !empty($field['Default']) ? $field['Default'] : '';
				if($field['Field']=='create_on'||$field['Field']=='update_on'){
					$fields[$field['Field']] = '';
				}
				if( strtolower($field['Key']) == 'pri' ){
					$pks[] = $field['Field'];
				}
			}
			if(count($pks)==1)$pks = $pks[0];
			$cache = array(
				'pk' => $pks,
				'fields'=> $fields,
			);
			$file = preg_replace("/^{$dbprefix}(\S+?)/", "\\1", $table);
			$cacheFile = $tmp_path.'/tmp/~table.'.$file.'.php';
			//if( is_writeable($cacheFile)){
				$content = 'return '.var_export( $cache,true ).";\n";
				$content  = "<?php\n".$content."\n?>";
				file_put_contents( $cacheFile,$content );
			//}
		}
	}
}